package com.hh.baseframework.util.base;

import com.hh.baseframework.util.MessageException;
import com.hh.baseframework.util.model.ReturnModel;
import com.hh.hibernate.util.base.BaseOneEntity;
import com.hh.system.service.impl.BaseService;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class BaseServiceAction<T extends BaseOneEntity> extends BaseAction<T>
		implements ModelDriven<T> {
	protected T object = null;

	public BaseServiceAction() {
		try {
			object = (T) getGenericType(0).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public T getModel() {
		return object;
	}

	public void queryPagingData() {
		this.returnResult(getService().queryPagingData(object,
				this.getPageRange()));
	}

	public void save() {
		try {
			T object = getService().save(this.object);
			this.getResultMap().put("object", object);
		} catch (MessageException e) {
			this.getResultMap().put("returnModel",
					new ReturnModel(e.getMessage()));
		}
		this.returnResult();
	}

	public void findObjectById() {
		T object = getService().findObjectById(this.object.getId());
		this.returnResult(object);
	}

	public void deleteByIds() {
		getService().deleteByIds(this.getIds());
	}

	public void order() {
		String id1 = request.getParameter("id1");
		String order1 = request.getParameter("order1");
		String id2 = request.getParameter("id2");
		String order2 = request.getParameter("order2");
		getService().updateOrder(id1, order1, id2, order2);
	}

	public BaseService<T> getService() {
		return null;
	}

}
